package com.example.myaop.aspect;

import com.example.myaop.annotation.LogAnnotation;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * 切面对象
 */
@Aspect
@Component
public class LogAspect {

    //找切点
    @Pointcut("@annotation(com.example.myaop.annotation.LogAnnotation)")
    public void logPointCut() {

    }

    //ProceedingJoinPoint 连接点对象
    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {

        //日志输出
        //方法名
        String name = point.getSignature().getName();
        // 通过反射获取方法对象
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        LogAnnotation annotation = method.getAnnotation(LogAnnotation.class);
        if (annotation != null) {
            System.out.println("【系统日志】当前操作："+annotation.value()+"调用了"+name+"方法，返回值是："+point.proceed());
        }
        return point.proceed();
    }
}
